<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>登录</title>
</head>
<body>
<input type="text" placeholder="用户名" id="username"> <br>
<input type="password" placeholder="密码" id="password"><br>
<button id="loginin" onclick="login()">登录</button>
<button id="registerin" onclick="register()">注册</button>
</body>
<script>
    function login(){
        const username = document.getElementById("username").value
        const password = document.getElementById("password").value
        if(!username||!password){
            alert("请输入用户名以及密码");
            return
        }
        fetch('/login',{
            method:"POST",
            headers:{
                "Content-Type":"application/json;charset=UTF-8"
            },
            body: JSON.stringify({username:username, password: password})
        }).then(res=>res.text()).then(res=>{

            if (res) {
                let json = JSON.parse(res)
                if(json.id){
                    location.href = '/index.html'
                }
            }
            else{
                alert("账号或密码错误")
            }
        })
    }
</script>
</html>